﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Constructor
{
    public partial class SubframePart : UserControl
    {
        Slot _slot;
        public SubframePart()
        {
            InitializeComponent();
        }
        public SubframePart(Slot slot)
        {
            InitializeComponent();
            _slot = slot;
            Enabled = slot.Frame.Name != "Объект";
            cb_can_be_null.Visible = slot.Type == CommonLib.SlotType.SubFrame;
            Init();
        }
        public bool CheckFrame(Frame frame)
        {
            if (frame.Name != _slot.Frame.Name)
            {
                Frame temp = frame.ParentFrame;
                while (temp != null)
                {
                    if (temp.Name == _slot.Frame.Name) return false;
                    temp = temp.ParentFrame;
                }
                return true;
            }
            return false;
        }
        public void Init()
        {
            foreach (Frame frame in External.frames)
            {
                if (CheckFrame(frame))
                    cmb_value.Items.Add(frame);
            }
            if (_slot.Subframe != null)
                cmb_value.Text = _slot.Subframe.Name;
            else
                if (cmb_value.Items.Count > 0)
                    cmb_value.SelectedIndex = 0;
            cb_can_be_null.Checked = _slot.CanBeNull; 
        }
        private void UpdateParentSlot()
        {
            Frame f = _slot.Frame;
            if (!_slot.isOwn && f.SearchSlot(_slot.Name) == null)
            {
                Slot s = _slot.Clone(f);
                f.Slots.Add(s);
                _slot = s;
            }
        }
        private void cmb_value_SelectedIndexChanged(object sender, EventArgs e)
        {
            Frame f = cmb_value.SelectedItem as Frame;
            if (f != null)
            {
                _slot.Subframe = f;
            }
            if (_slot.Type == CommonLib.SlotType.SubFrame) UpdateParentSlot();
        }

        private void cb_can_be_null_CheckedChanged(object sender, EventArgs e)
        {
            _slot.CanBeNull = cb_can_be_null.Checked;
        }
    }
}
